package com.jerkin.easyexcl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.StringUtils;

import com.google.common.collect.Lists;
import com.jerkin.easyexcl.model.InvoiceDetail;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @version V1.0
 * @ClassName: ReadListener
 * @Description:
 * @author: huhongzhi
 * @date: 2020/9/21 15:37
 */
public class ReadListener extends AnalysisEventListener<InvoiceDetail> {

    private static final List<InvoiceDetail> sale = new ArrayList<>();

    private static final List<InvoiceDetail> returns = new ArrayList<>();

    private static final List<String> values = Lists.newArrayList("109930");


    @Override
    public void invoke(InvoiceDetail invoiceDetail, AnalysisContext analysisContext) {
        String sheetName = analysisContext.readSheetHolder().getReadSheet().getSheetName();
//        invoiceDetail.setPrice(invoiceDetail.getPrice().divide(BigDecimal.valueOf(100),2));
//        invoiceDetail.setTotalPrice(invoiceDetail.getTotalPrice().divide(BigDecimal.valueOf(100),2));
        if (Objects.equals(invoiceDetail.getTypeNo(), "951")
                || Objects.equals(invoiceDetail.getTypeNo(), "405")
                || Objects.equals(invoiceDetail.getTypeNo(), "842")) {
            invoiceDetail.setTypeName(invoiceDetail.getNickName());
        }
        if(StringUtils.isEmpty(invoiceDetail.getBizNo())){
            return;
        }
        if (Objects.equals("销售单", sheetName)) {
            invoiceDetail.setType(1);
            isOil(invoiceDetail);
            sale.add(invoiceDetail);
        } else {
            invoiceDetail.setType(2);
            isOil(invoiceDetail);
            returns.add(invoiceDetail);
        }

    }

    private void isOil(InvoiceDetail invoiceDetail) {
        if (Objects.equals(invoiceDetail.getTypeNo1(), "9")
                && !Objects.equals(invoiceDetail.getTypeNo(), "32")
                && values.indexOf(invoiceDetail.getKzSkuCode()) == -1) {
            invoiceDetail.setFlag("是");
            String accNum = invoiceDetail.getAccNum().split("/")[0];
            invoiceDetail.setOilUnit(OilTypeEnum.initUnit(accNum));
            invoiceDetail.setAmount(OilTypeEnum.initAmount(accNum).toString());
        } else {
            invoiceDetail.setFlag("否");
        }
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public static List<InvoiceDetail> getSales() {
        return sale;
    }

    public static List<InvoiceDetail> getReturns() {
        return returns;
    }
}
